package com.cxyzy.myapplication

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        GlobalScope.launch {
            doSomething()
            withContext(Dispatchers.IO)
            {
                textView.text = "coroutines完成执行"
                Toast.makeText(this@MainActivity, "coroutines hooray", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun doSomething() {
        Thread.sleep(5000)
    }
}
